import 'dart:io';

import 'package:aichat_flutter_wallet/module/module_create_wallet/model/mnemonic.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/mnemonic_tips_page.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/verify_mnemonic_page.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/button/bottom_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/header/page_header.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_language/lib_language.dart';

class CreateMnemonicPage extends StatefulWidget {
  final int wallet_id;

  const CreateMnemonicPage({super.key, required this.wallet_id});

  @override
  State<CreateMnemonicPage> createState() => _CreateMnemonicPageState();
}

class _CreateMnemonicPageState extends State<CreateMnemonicPage> {
  late List<String> mnemomicList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getWalletMnemonic();
  }

  getWalletMnemonic() {
    // 解密
    mnemomicList = MnemonicData.getMnemonicList(widget.wallet_id);
  }

  // 跳过
  doSkip() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => MnemonicTipsPage()));
  }

  // 校验
  doCheckBackup() {
    MnemonicData().setWalletId(widget.wallet_id);
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => VerifyMnemonicPage()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: Stack(children: [
          Column(
            children: [
              PageHeaderCom(title: LocaleKeys.backup_recovery_phrase.tr(), showGoBackBtn: false),
              Container(
                padding: EdgeInsets.only(top: 10.w, left: 20.w, right: 20.w),
                child: Text(
                  'Please write down the recovery phrase correctly and store it securely.Avoid online storage to prevent leaks.',
                  style: TextStyle(fontSize: 12.sp, color: AppColors.c_333333),
                ),
              ),
              SizedBox(
                height: 20.w,
              ),
              Container(
                padding: EdgeInsets.only(top: 10.w, bottom: 5.w, right: 15.w, left: 15.w),
                width: double.infinity,
                margin: EdgeInsets.symmetric(horizontal: 20.w),
                decoration: BoxDecoration(color: AppColors.c_F6F6F6, borderRadius: BorderRadius.circular(10.w)),
                child: Wrap(
                  alignment: WrapAlignment.spaceEvenly,
                  children: List.generate(mnemomicList.length, (index) {
                    return GestureDetector(
                      onTap: () {
                        // Kprint('$index');
                      },
                      child: Container(
                        // height: Platform.isAndroid ? 47 : 51,

                        // ///7320
                        width: 90.w,
                        margin: EdgeInsets.only(bottom: 10.w),
                        padding: EdgeInsets.only(left: 10.w, top: 5.w, bottom: 5.w),
                        decoration: BoxDecoration(color: AppColors.c_white, borderRadius: BorderRadius.circular(10.w)),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('${index + 1}',
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  fontWeight: FontWeight.w500,
                                  color: AppColors.c_BEBDCA,
                                )),
                            Text(
                              "${mnemomicList[index]}",
                              style: TextStyle(fontSize: 14.sp, color: AppColors.c_333333, fontWeight: FontWeight.w400),
                            ),
                          ],
                        ),
                      ),
                    );
                  }),
                ),
              ),
            ],
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Padding(
              padding: EdgeInsets.fromLTRB(20.w, 13.w, 20.w, MediaQuery.of(context).padding.bottom + 11).w,
              child: BottomBtn(
                isEvent: false,
                textList: ['Skip', 'Cehck BackUp'],
                onCancel: () {
                  doSkip();
                },
                onConfirm: () {
                  doCheckBackup();
                },
              ),
            ),
          )
        ]),
      ),
    );
  }
}
